// pages/home/home.js

import {http} from "../../utils/index"
const app = getApp();
var QQMapWX = require('../../utils/qqmap-wx-jssdk.min.js');
var qqmapsdk;

Page({

  /**
   * 页面的初始数据
   */
  data: {
    province:null,
    city:"",
    cityList:[],
    observe:[],
    fore:[]
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    qqmapsdk = new QQMapWX({
      key: 'P7CBZ-5DMKG-YNLQN-IJ7VF-WIDA7-K7FCM' 
    });
    // this.obser("CH200101")
    // this.weather("CH200101")
    // this.forecast("CH200101")
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    let vm = this;
    vm.getUserLocation();
  },

  
  getUserLocation: function () {

    let vm = this;
    vm.getLocation();
    // wx.getSetting({

    //   success: (res) => {
    //     console.log(res)
        

    //     // if (res.authSetting['scope.userLocation'] != undefined && res.authSetting['scope.userLocation'] != true) {

    //       wx.showModal({

    //         title: '请求授权当前位置',

    //         content: '需要获取您的地理位置，请确认授权',

    //         success: function (res) {
    //           console.log(res)

    //           if (res.cancel) {

    //             wx.showToast({

    //               title: '拒绝授权',

    //               icon: 'none',

    //               duration: 1000

    //             })

    //           } else if (res.confirm) {

    //             wx.openSetting({
    //               success:dataAu => {
    //                 console.log(dataAu)
    //                 console.log(1)
    //                 if (dataAu.authSetting["scope.userLocation"] == true) {

    //                   wx.showToast({

    //                     title: '授权成功',

    //                     icon: 'success',

    //                     duration: 1000

    //                   })
    //                   vm.getLocation();

    //                 } else {

    //                   wx.showToast({

    //                     title: '授权失败',

    //                     icon: 'none',

    //                     duration: 1000

    //                   })

    //                 }

    //               }

    //             })

    //           }

    //         }

    //       })

        // } else if (res.authSetting['scope.userLocation'] == undefined) {

        //   //调用wx.getLocation的API

        

        // }

        // else {

        //   //调用wx.getLocation的API

        //   vm.getLocation();

        // }

      // }

    // })

  },

  // 微信获得经纬度

  getLocation: function () {

    let vm = this;

    wx.getLocation({

      type: 'wgs84',

      success: function (res) {

        // console.log(JSON.stringify(res))

        var latitude = res.latitude

        var longitude = res.longitude

        vm.getLocal(latitude, longitude)

      },

      fail: function (res) {

        console.log('fail' + JSON.stringify(res))

      }

    })

  },

  // 获取当前地理位置

  getLocal: function (latitude, longitude) {

    let vm = this;

    qqmapsdk.reverseGeocoder({

      location: {

        latitude: latitude,

        longitude: longitude

      },

      success: function (res) {
        let province = res.result.ad_info.province

        let city = res.result.ad_info.city

        wx.setStorageSync('province', province)
        wx.setStorageSync('city', city)
        vm.setData({
          city: city,
          province:province,
        })

        vm.filt(city)

      },

      fail: function (res) {

        console.log(res);

      },

      complete: function (res) {

      }

    });

  },

  filt:function(city){
    var that = this
    var city = city.slice(0,city.length-1) 
    var lis = null
    http({
      url:"https://api.yytianqi.com/citylist/id/1",
      success:res=>{
        res.data.list.forEach(item=>{
          item.list.forEach(item=>{
             if(item.name == city){
               if(item.list){
                lis = item.list[0]
               }else{
                lis = item
               }
             }
          })
        })
        this.obser(lis.city_id)
        this.weather(lis.city_id)
        this.forecast(lis.city_id)
      }
    })
  },

  forecast:function(id){
    http({
      url:"https://api.yytianqi.com/forecast7d",
      data:{
        city:id,
        key:"dufs7a88oasqfdi0"  
      },
      success:res=>{
          console.log(res.data.data.list)
          res.data.data.list.forEach(item=>{
            item.date = item.date.slice(5,item.date.length)
          })
          this.setData({
            fore:res.data.data.list
          })
      }
    })
  },

  weather:function(id){
    http({
      url:"https://api.yytianqi.com/weatherhours",
      data:{
        city:id,
        key:"dufs7a88oasqfdi0"  
      },
      success:res=>{
        res.data.data.list.forEach(item=>{
          item.sj = item.sj.slice(11,16)
          if(item.sj>"05:00"&&item.sj<"19:00"){
            item.number = 0
          }else{
            item.number = 1
          }
        })

          this.setData({
            cityList:res.data.data.list
          })
      }
    })
  },

  obser:function(id){
    http({
      url:"https://api.yytianqi.com/observe",
      data:{
        city:id,
        key:"dufs7a88oasqfdi0"  
      },
      success:res=>{
        res.data.data.lastUpdate = res.data.data.lastUpdate.split(" ")
        if(res.data.data.lastUpdate[1]>"05:00:00"&&res.data.data.lastUpdate[1]<"19:00:00"){
          res.data.data.number = 0
        }else{
          res.data.data = 1
        }
        this.setData({
          observe:res.data.data
        })
      }
    })
  },



  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})